<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动ID" prop="activityId">
        <el-input
          v-model="queryParams.activityId"
          placeholder="请输入活动ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="activityTitle">
        <el-input
          v-model="queryParams.activityTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动类型" prop="activityTypeId">
        <el-select v-model="queryParams.activityTypeId" placeholder="请选择活动类型" clearable>
          <el-option
            v-for="dict in dict.type.activity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="activityStartTime">
        <el-date-picker clearable
          v-model="queryParams.activityStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="activityEndTime">
        <el-date-picker clearable
          v-model="queryParams.activityEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发起人" prop="activityInitiator" >
        <el-input
          v-model="queryParams.activityInitiator"
          placeholder="请输入发起人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发起人ID" prop="activityInitiatorId">
        <el-input
          v-model="queryParams.activityInitiatorId"
          placeholder="请输入发起人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审核意见" prop="activityReviewComment">
        <el-input
          v-model="queryParams.activityReviewComment"
          placeholder="请输入审核意见"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="activityReviewStatus">
        <el-select v-model="queryParams.activityReviewStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.activity_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:checkActivities:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:checkActivities:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkActivitiesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="activityId" />
      <el-table-column label="标题" align="center" prop="activityTitle" />
      <el-table-column label="活动类型" align="center" prop="activityTypeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_type" :value="scope.row.activityTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="activityStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="activityEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进行状态" align="center" prop="activityProgress">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_progress" :value="scope.row.activityProgress"/>
        </template>
      </el-table-column>
      <el-table-column label="发起人" align="center" prop="activityInitiator" />
      <el-table-column label="发起时间" align="center" prop="activityInitiationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityInitiationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="activityUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="activityReviewer" />
      <el-table-column label="审核时间" align="center" prop="activityReviewTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityReviewTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="activityReviewComment" />
      <el-table-column label="审核状态" align="center" prop="activityReviewStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_status" :value="scope.row.activityReviewStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:checkActivities:edit']"
          >修改</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动标题" prop="activityTitle">
          <el-input v-model="form.activityTitle" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="活动内容">
          <editor v-model="form.activityContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="活动类型" prop="activityTypeId">
          <el-select v-model="form.activityTypeId" placeholder="请选择活动类型">
            <el-option
              v-for="dict in dict.type.activity_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="activityStartTime">
          <el-date-picker clearable
            v-model="form.activityStartTime"
            type="datetime"
            format="yyyy年MM月dd  HH:mm"
            value-format="yyyy-MM-dd HH:mm:ss"
            popper-class="no-atTheMoment"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="结束时间" prop="activityEndTime">
          <el-date-picker clearable
            v-model="form.activityEndTime"
            type="datetime"
            format="yyyy年MM月dd  HH:mm"
            value-format="yyyy-MM-dd HH:mm:ss"
            popper-class="no-atTheMoment"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发起人" prop="activityInitiator">
          <el-input v-model="form.activityInitiator" placeholder="请输入发起人" />
        </el-form-item>
        <el-form-item label="发起人ID" prop="activityInitiatorId">
          <el-input v-model="form.activityInitiatorId" placeholder="请输入发起人ID" />
        </el-form-item>

        <el-form-item label="审核意见" prop="activityReviewComment">
          <el-input v-model="form.activityReviewComment" placeholder="请输入审核意见" />
        </el-form-item>
        <el-form-item label="审核状态" prop="activityReviewStatus">
          <el-select v-model="form.activityReviewStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.activity_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCheckActivities, getCheckActivities, delCheckActivities, addCheckActivities, updateCheckActivities } from "@/api/activity/checkActivities";
import {getToken} from "@/utils/auth";

export default {
  name: "CheckActivities",
  dicts: ['activity_status', 'activity_type', 'activity_progress'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动审核表格数据
      checkActivitiesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态时间范围
      daterangeActivityInitiationTime: [],
      // 审核状态时间范围
      daterangeActivityUpdateTime: [],
      // 审核状态时间范围
      daterangeActivityReviewTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: null,
        activityTitle: null,
        activityTypeId: null,
        activityStartTime: null,
        activityEndTime: null,
        activityInitiator: null,
        activityInitiatorId: null,
        activityReviewer: null,
        activityReviewTime: null,
        activityReviewComment: null,
        activityReviewStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

        activityReviewComment: [
          { required: true, message: "审核意见不能为空", trigger: "blur" }
        ],
        activityReviewStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ]
      },
      // 导入参数
      upload: {
          // 是否显示弹出层（导入）
          open: false,
                  // 弹出层标题（导入）
                  title: "",
                  // 是否禁用上传
                  isUploading: false,
                  // 设置上传的请求头部
                  headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/activity/checkActivities/importData"
      }

  };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询活动审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeActivityInitiationTime && '' != this.daterangeActivityInitiationTime) {
        this.queryParams.params["beginActivityInitiationTime"] = this.daterangeActivityInitiationTime[0];
        this.queryParams.params["endActivityInitiationTime"] = this.daterangeActivityInitiationTime[1];
      }
      if (null != this.daterangeActivityUpdateTime && '' != this.daterangeActivityUpdateTime) {
        this.queryParams.params["beginActivityUpdateTime"] = this.daterangeActivityUpdateTime[0];
        this.queryParams.params["endActivityUpdateTime"] = this.daterangeActivityUpdateTime[1];
      }
      if (null != this.daterangeActivityReviewTime && '' != this.daterangeActivityReviewTime) {
        this.queryParams.params["beginActivityReviewTime"] = this.daterangeActivityReviewTime[0];
        this.queryParams.params["endActivityReviewTime"] = this.daterangeActivityReviewTime[1];
      }
      listCheckActivities(this.queryParams).then(response => {
        this.checkActivitiesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        activityTitle: null,
        activityContent: null,
        activityTypeId: null,
        activityStartTime: null,
        activityEndTime: null,
        activityProgress: null,
        activityInitiator: null,
        activityInitiatorId: null,
        activityInitiationTime: null,
        activityUpdateTime: null,
        activityReviewer: null,
        activityReviewTime: null,
        activityReviewComment: null,
        activityReviewStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeActivityInitiationTime = [];
      this.daterangeActivityUpdateTime = [];
      this.daterangeActivityReviewTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const activityId = row.activityId || this.ids
      getCheckActivities(activityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "活动审核信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.activityId != null) {
            updateCheckActivities(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckActivities(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/checkActivities/export', {
        ...this.queryParams
      }, `checkActivities_${new Date().getTime()}.xlsx`)
    },

  }
};
</script>
